<?php
namespace app\member\controller;

use think\Controller;

class Member extends Controller
{

    public function _empty()
    {
        $this->error('当前页面不存在', controller('Index/index'));
    }

    /**
     * 构造函数    2017-10-15
     */
    protected function _initialize()
    {
        $this->login();    //获取登录信息
        $loginer = session('loginer_auth');
        if (!in_array(strtolower(request()->controller()), array('index', 'user', 'member'))) {
            if ($loginer['rank'] != 1) {
                $group = db(\tname::auth_group)->find($loginer['type']);

                $controller_action = BIND_MODULE . '.php/' . strtolower(request()->controller()) . '/' . strtolower(request()->action());
                $auths = db(\tname::auth_rule)->where(array('id' => array('in', $group['rules'])))->column('name');

                $auths_group = db(\tname::auth_rule)->where(array('id' => array('in', $group['rules'])))->column('group_name');
                $auths_group = array_unique($auths_group);
                $auths_group = implode(',', $auths_group);


                if (!in_array($controller_action, $auths)) {
                    $this->error('未授权访问!');
                }
            }
        }
        isset($auths_group) ? $auths_group : $auths_group = 'all';
        $this->assign('auths_group', $auths_group);

    }

    /**
     * 获取登录信息    2017-10-15
     */
    protected function login()
    {
        $loginer = session('loginer_auth');
        $user = session('user_auth');
        define('LID', $loginer['id']);
        define('UID', $user['id']);

        if (!UID) {
            return $this->redirect('User/index');
        }
    }

    /**
     * 针对某表某字段进行更改    2017-10-15
     */
    public function updatefield()
    {
        $data = input('post.');

        $updatedata = array(
            'id' => $data['id'],
            $data['fieldname'] => $data['afterchange']
        );
        $res = dataUpdate($data['tablename'], $updatedata);

        if (!$res) {
            return ajaxFalse();
        }
        return ajaxSuccess('', '');
    }

    /**
     * 百度上传图片插件返回路径    2017-10-15
     */
    public function webfileupload()
    {
        $files = request()->file('');

        foreach ($files as $file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'picture' . DS . 'uid' . UID . DS . 'member');

            if ($info) {
                $savename = str_replace('\\', '/', $info->getSaveName());
                $arr = array(
                    'imgpath' => '/uploads/picture/uid' . UID . '/member/' . $savename,
                    'status' => 1,
                );
                return json($arr);
            } else {
                // 上传失败获取错误信息
                return $file->getError();
            }
        }
    }

}
